<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="企业" prop="enterprise.id"
                :rules="[
                  {required: true, message:'企业不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择企业"
            labelName = 'companyName'
            labelValue = 'id'
            :value = "inputForm.enterprise.id"
            :limit="1"
            @getValue='(value) => {inputForm.enterprise.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
            entityBeanName="enterpriseInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="部门" prop="department.id"
                :rules="[
                  {required: true, message:'部门不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="department"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

                      url="/dlyrl/hr/orporateinstitutions/orporateInstitutions/treeData"
                      :value="inputForm.department.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.department.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="岗位" prop="job"
                :rules="[
                  {required: true, message:'岗位不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.job" placeholder="请填写岗位"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用户" prop="userinformation.id"
                :rules="[
                  {required: true, message:'用户不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择用户"
            labelName = 'userName'
            labelValue = 'id'
            :value = "inputForm.userinformation.id"
            :limit="1"
            @getValue='(value) => {inputForm.userinformation.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'userName',
              label: '用户名'
            }
            ]"
            dataListUrl="/dlyrl/hr/userinformation/userInformation/list"
            entityBeanName="userInformation"
            queryEntityUrl="/dlyrl/hr/userinformation/userInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="打卡时间" prop="checkinTime"
                :rules="[
                  {required: true, message:'打卡时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.checkinTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="经度" prop="longitude"
                :rules="[
                  {required: true, message:'经度不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.longitude" placeholder="请填写经度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="纬度" prop="latitude"
                :rules="[
                  {required: true, message:'纬度不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.latitude" placeholder="请填写纬度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="打卡地址" prop="area"
                :rules="[
                  {required: true, message:'打卡地址不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.area" placeholder="请填写打卡地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="打卡状态" prop="checkinType"
                :rules="[
                  {required: true, message:'打卡状态(0:正常;1:迟到;2:早退;3:未打卡)不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.checkinType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('checkin_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签到签退" prop="isType"
                :rules="[
                  {required: true, message:'签到签退不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.isType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('is_checkin')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="正常打卡时间" prop="normalTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.normalTime" placeholder="请填写正常打卡时间"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="温度" prop="animalHeat"
                :rules="[
                  {required: true, message:'温度不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.animalHeat" placeholder="请填写温度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="每日加班时长" prop="dailyovertime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.dailyovertime" placeholder="请填写每日加班时长"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="总加班时长" prop="allovertime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.allovertime" placeholder="请填写总加班时长"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工作时段" prop="workingHours"
                :rules="[
                  {required: true, message:'工作时段不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.workingHours" placeholder="请填写工作时段"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="设备标识" prop="equipment"
                :rules="[
                  {required: true, message:'设备标识不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.equipment" placeholder="请填写设备标识"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="ip地址" prop="ipaddress"
                :rules="[
                  {required: true, message:'ip地址不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.ipaddress" placeholder="请填写ip地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工作区间" prop="workingTime"
                :rules="[
                  {required: true, message:'工作区间不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.workingTime" placeholder="请填写工作区间"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          enterprise: {
            id: ''
          },
          department: {
            id: ''
          },
          job: '',
          userinformation: {
            id: ''
          },
          checkinTime: '',
          longitude: '',
          latitude: '',
          area: '',
          checkinType: '',
          isType: '',
          normalTime: '',
          animalHeat: '',
          dailyovertime: '',
          allovertime: '',
          workingHours: '',
          equipment: '',
          ipaddress: '',
          workingTime: '',
          remarks: ''
        }
      }
    },
    components: {
      GridSelect,
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建打卡记录`
        } else if (method === 'edit') {
          this.title = '修改打卡记录'
        } else if (method === 'view') {
          this.title = '查看打卡记录'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/checkinrecord/checkinRecord/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.checkinRecord)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/checkinrecord/checkinRecord/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>